package dkbta.ontology.elements.event;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;

import dkbta.ontology.elements.Element;
import dkbta.ontology.elements.ElementType;
import dkbta.util.time.TimeInterval;

public class Event extends Element{
	/** The values and their types of the attributes */
	private final Map<String, EventAttribute> _attributeValues;

	/**
	 * Initializes an event instance with the given parameters
	 * 
	 * @param name The element's name
	 * @param startTime The element's start time
	 * @param endTime The element's end time
	 * @param attributeValues The values of the event's attributes
	 */
	public Event(String name, long startTime, long endTime,
		Map<String, EventAttribute> attributeValues){
		super(ElementType.EVENT, name, startTime, endTime, null, null);
		_attributeValues = attributeValues;
	}

	/**
	 * Initializes an event instance with the given parameters
	 * 
	 * @param name The element's name
	 * @param timeInterval The time interval
	 * @param attributeValues The values of the event's attributes
	 */
	public Event(String name, TimeInterval timeInterval,
		Map<String, EventAttribute> attributeValues){
		super(ElementType.EVENT, name, timeInterval, null, null);
		_attributeValues = attributeValues;
	}

	/**
	 * Returns the value of the given attribute
	 * 
	 * @param attributeName The name of one of the event's attributes
	 * @return The value of the given attribute
	 */
	public EventAttribute getAttribute(String attributeName){
		return _attributeValues == null ? null : _attributeValues.get(attributeName);
	}

	/**
	 * Returns a string representation of the instance
	 * 
	 * @return A string representation of the instance
	 */
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder(super.toString());
		if (_attributeValues != null && !_attributeValues.isEmpty()){
			sb.append(" = ");
			Iterator<EventAttribute> iter = _attributeValues.values().iterator();
			while (iter.hasNext()){
				sb.append(iter.next().getValue());
				if (iter.hasNext()){
					sb.append(",");
				}
			}
		}
		return sb.toString();
	}

	@Override
	public void insertDataIntoPS(PreparedStatement ps, int valueIndex, int typeIndex)
			throws SQLException{
		ps.setString(valueIndex, "1");
		ps.setString(typeIndex, "EVENT");
	}
}
